package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Admin;
import com.example.springboot.domain.po.User;
import com.example.springboot.service.AccountService;
import com.example.springboot.service.AdminService;
import com.example.springboot.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：AccountServiceImpl
 * @Date：2025/2/6 21:35
 * @Filename：AccountServiceImpl
 * @for:
 */
@Service
public class AccountServiceImpl implements AccountService {
       @Resource
       private UserService userService;
       @Resource
       private AdminService adminService;
       @Override
       public Map<String, Object> JWTLogin(Account account) {
              Integer role = account.getRole();
              Account login = new Account();
              //如果登录角色为1，说明是管理原登录，把用户信息到管理员表中去校验账号名和密码
              if (role == 1) {
                     //转成Admin类
                     Admin admin = BeanUtil.copyProperties(account, Admin.class);
                     login = adminService.adminLogin(admin);
              } else if (role == 2) {
                     //转成User类
                     User user = BeanUtil.copyProperties(account, User.class);
                     login = userService.userLogin(user);
              }
              //        生成token
              String token = JwtTokenUtils.genToken(login.getId() + "-" + login.getRole(), login.getPassword());
              //        创建一个键值对map集合，把token和user塞进去，返回给前端
              Map<String, Object> map = new HashMap<>();
              map.put("token", token);
              map.put("user", login);
              return map;
       }
}
